package com.ruoyi.web.controller.construction.supertube;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.supe.DistributionTeam;
import com.ruoyi.system.service.customer.DistributionTeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/system/distribution")
public class DistributionTeamController extends BaseController
{
    @Autowired
    private DistributionTeamService distributionTeamService;

    /**
     * 查询商家分销列表
     */
    @PreAuthorize("@ss.hasPermi('distribution:team:list')")
    @GetMapping("/list")
    public TableDataInfo list(DistributionTeam distributionTeam)
    {
        startPage();
        List<DistributionTeam> list = distributionTeamService.selectDistributionTeamList(distributionTeam);
        return getDataTable(list);
    }

    /**
     * 导出商家分销列表
     */
    @PreAuthorize("@ss.hasPermi('distribution:team:export')")
    @Log(title = "商家分销", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DistributionTeam distributionTeam)
    {
        List<DistributionTeam> list = distributionTeamService.selectDistributionTeamList(distributionTeam);
        ExcelUtil<DistributionTeam> util = new ExcelUtil<DistributionTeam>(DistributionTeam.class);
        return util.exportExcel(list, "team");
    }

    /**
     * 获取商家分销详细信息
     */
    @PreAuthorize("@ss.hasPermi('distribution:team:query')")
    @GetMapping(value = "/{distributionId}")
    public AjaxResult getInfo(@PathVariable("distributionId") Long distributionId)
    {
        return AjaxResult.success(distributionTeamService.selectDistributionTeamById(distributionId));
    }

    @PreAuthorize("@ss.hasPermi('distribution:team:supplierId')")
    @GetMapping(value = "/supplierIdById/{supplierId}")
    public List<DistributionTeam> supplierIdById(@PathVariable("supplierId") Long supplierId)
    {
        List<DistributionTeam> distributionTeams = distributionTeamService.supplierIdById(supplierId);
        return distributionTeams;
    }



    /**
     * 新增商家分销
     */
    @PreAuthorize("@ss.hasPermi('distribution:team:add')")
    @Log(title = "商家分销", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody DistributionTeam distributionTeam)
    {
        if (UserConstants.NOT_UNIQUE.equals(distributionTeamService.checksupplierIdUnique(distributionTeam.getSupplierId())))
        {
            System.err.println("**************xianding**************");
            return AjaxResult.error("新增用户'" + distributionTeam.getSupplierId() + "'失败，登录账号已存在");
        }

        if (UserConstants.NOT_UNIQUE.equals(distributionTeamService.checkPhoneUnique(distributionTeam)))
        {
            return AjaxResult.error("修改用户'" + distributionTeam.getSupplierId() + "'失败，手机号码已存在");
        }
        return toAjax(distributionTeamService.insertDistributionTeam(distributionTeam));
    }

    /**
     * 修改商家分销
     */
    @PreAuthorize("@ss.hasPermi('distribution:team:edit')")
    @Log(title = "商家分销", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DistributionTeam distributionTeam)
    {
        return toAjax(distributionTeamService.updateDistributionTeam(distributionTeam));
    }

    /**
     * 删除商家分销
     */
    @PreAuthorize("@ss.hasPermi('distribution:team:remove')")
    @Log(title = "商家分销", businessType = BusinessType.DELETE)
    @DeleteMapping("/{distributionIds}")
    public AjaxResult remove(@PathVariable Long[] distributionIds)
    {
        return toAjax(distributionTeamService.deleteDistributionTeamByIds(distributionIds));
    }
}
